#!/bin/bash
set -e

export DEBIAN_FRONTEND=noninteractive
export CLANG_VERSION=12

sed -i 's/# deb-src/deb-src/g' /etc/apt/sources.list
apt-get update
apt-get dist-upgrade -y
apt-get install -y --no-install-recommends \
  ca-certificates \
  git \
  wget \
  python3 \
  python3-pip \
  python3-dev \
  gcc \
  clang-$CLANG_VERSION \
  cmake \
  libatk1.0-dev \
  libatk-bridge2.0-dev \
  libatspi2.0-dev \
  libcups2-dev \
  libxcomposite-dev \
  libxdamage-dev \
  libunwind-dev \
  libdw-dev \
  libgmp-dev \
  libgraphene-1.0-dev \
  libgsl-dev \
  libglib2.0-dev \
  flex \
  bison \
  libcap-dev \
  libgirepository1.0-dev \
  gettext \
  liborc-0.4-dev \
  iso-codes \
  libgl-dev \
  libgles-dev \
  libdrm-dev \
  libgudev-1.0-dev \
  libgbm-dev \
  libpng-dev \
  libjpeg-dev \
  libogg-dev \
  libopus-dev \
  libpango1.0-dev \
  libvisual-0.4-dev \
  libtheora-dev \
  libvorbis-dev \
  libxkbcommon-dev \
  libwayland-dev \
  libepoxy-dev \
  ruby \
  libgcrypt20-dev \
  libwebp-dev \
  libopenjp2-7-dev \
  libwoff-dev \
  libxslt1-dev \
  bubblewrap \
  libseccomp-dev \
  xdg-dbus-proxy \
  gperf \
  libsoup2.4-dev \
  libvulkan-dev \
  libass-dev \
  libchromaprint-dev \
  libcurl4-gnutls-dev \
  libaom-dev \
  libbz2-dev \
  liblcms2-dev \
  libbs2b-dev \
  libdca-dev \
  libfaac-dev \
  libfaad-dev \
  flite1-dev \
  libssl-dev \
  libfdk-aac-dev \
  libfluidsynth-dev \
  libgsm1-dev \
  libkate-dev \
  libgme-dev \
  libde265-dev \
  liblilv-dev \
  libmodplug-dev \
  libmjpegtools-dev \
  libmpcdec-dev \
  libdvdnav-dev \
  libdvdread-dev \
  librsvg2-dev \
  librtmp-dev \
  libsbc-dev \
  libsndfile1-dev \
  libsoundtouch-dev \
  libspandsp-dev \
  libsrt-gnutls-dev \
  libsrtp2-dev \
  libvo-aacenc-dev \
  libvo-amrwbenc-dev \
  libwebrtc-audio-processing-dev \
  libofa0-dev \
  libzvbi-dev \
  libopenexr-dev \
  libwildmidi-dev \
  libx265-dev \
  libzbar-dev \
  wayland-protocols \
  libaa1-dev \
  libmp3lame-dev \
  libcaca-dev \
  libdv4-dev \
  libmpg123-dev \
  libvpx-dev \
  libshout3-dev \
  libspeex-dev \
  libtag1-dev \
  libtwolame-dev \
  libwavpack-dev \
  liba52-0.7.4-dev \
  libx264-dev \
  libopencore-amrnb-dev \
  libopencore-amrwb-dev \
  libmpeg2-4-dev \
  libavcodec-dev \
  libavfilter-dev \
  libavformat-dev \
  libavutil-dev \
  libva-dev \
  libudev-dev \
  glibc-tools \
  libqrencode-dev \
  libjson-glib-dev
pip3 install meson ninja hotdoc

ARCH=$(dpkg --print-architecture)
if [[ $ARCH == "amd64" ]]; then
   apt-get install -y --no-install-recommends \
     libmfx-dev \
     intel-media-va-driver-non-free
fi

apt-get clean
rm -rf /var/lib/apt/lists/*

# SCCache can be used to speed-up WPE compilation. You need cloud builders setup
# though. SCCache binaries are available for arm64 but don't support toolchain
# packaging, so skip this when building on arm64.
if [[ $ARCH == "amd64" ]]; then
  MACHINE=$(uname -m)
  export SCCACHE_VERSION="0.2.14"
  wget https://github.com/mozilla/sccache/releases/download/$SCCACHE_VERSION/sccache-$SCCACHE_VERSION-$MACHINE-unknown-linux-musl.tar.gz
  tar xf sccache-$SCCACHE_VERSION-$MACHINE-unknown-linux-musl.tar.gz
  mv sccache-$SCCACHE_VERSION-$MACHINE-unknown-linux-musl/sccache /usr/bin
  rm -rf sccache-$SCCACHE_VERSION-$MACHINE-unknown-linux-musl

  ln -s /usr/bin/clang{-$CLANG_VERSION,}
  ln -s /usr/bin/clang++{-$CLANG_VERSION,}

  sccache --package-toolchain /usr/bin/clang-$CLANG_VERSION /toolchain-clang-$CLANG_VERSION.tar.gz
fi

wget -O- https://sh.rustup.rs | sh -s -- -y --default-toolchain stable
